package matchPattern

import scala.util.Random

object MatchApp extends App {
  var names = Array("a", "b", "c")
  var lname = List("a", "b", "c")
  var name = names(Random.nextInt(names.length))
  name match {
    case "a" => println("aaa")
    case "b" => println("bbb")
    case "c" => println("ccc")
    case _ => println("ddd")
  }

  def judgeGrade(name:String, grade:String): Unit ={
    grade match{
      case "A" => println("Best")
      case "B" => println("Good")
      case "C" => println("General")
      case _ if name == "lisi" => println(name + " is Bad")     // case 后添加条件过滤
      case _ => println("Bad")
    }
  }
  judgeGrade("lisi","D")

  def greeting(array:Array[String]): Unit ={
    array match {
      case Array("a") => println("aqie")
      case Array(x, y) => println("hi:" + x + " ," + y)
      case Array("a", _*) => println("Hi: aqie and other friends")
      case _ => println("Hi everyone")
    }
  }

  greeting(names)

  def greeting(list:List[String]): Unit ={
    list match {
      case "a"::Nil => println("aqie")
      case x::y::Nil => println("hello" + x + y)
      case "a"::tail => println("hello everyone")
    }
  }
  greeting(lname)

  // 类型匹配
  def matchType(obj:Any): Unit ={
    obj match {
      case x:Int => println("Int")
      case s:Int => println("String")
      case m:Map[_, _] => m.foreach(println)
      case _ => println("other type")
    }
  }
  matchType("1")
  matchType(1)
  matchType(1l)
  matchType(1f)
  matchType(Map("name" -> "aqie"))
}
